package com.supeng.PMdepot.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.supeng.PMdepot.ItemDecoration.SpaceItemDecoration;
import com.supeng.PMdepot.R;
import com.supeng.PMdepot.adapter.SearchAdapter;
import com.supeng.PMdepot.base.BaseActivity;
import com.supeng.PMdepot.common.Constant;
import com.supeng.PMdepot.entity.RukuBeen;
import com.supeng.PMdepot.entity.SearchBeenInfo;
import com.supeng.PMdepot.entity.ServiceResult;
import com.supeng.PMdepot.net.Api;
import com.supeng.PMdepot.net.NetUtils;
import com.supeng.PMdepot.util.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SearchActivity extends BaseActivity {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.title_bar)
    RelativeLayout titleBar;
    @BindView(R.id.et_kucun)
    EditText etKucun;
    @BindView(R.id.vv_blue3)
    View vvBlue3;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.root_layout)
    LinearLayout rootLayout;
//    @BindView(R.id.tv_cwtm)
//    TextView tvCwtm;
//    @BindView(R.id.tv_cpCount)
//    TextView tvCpCount;
    private SearchAdapter homeAdapter;
  //  public String cangwei;

    @BindView(R.id.tv_count)
    TextView tvCount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //弹出键盘滚动问题
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_search);
        unbinder = ButterKnife.bind(this);
        initView();
        initData();
        initAdapter();
    }

    private boolean actionOk(TextView v) {
//        if (!TextUtils.isEmpty(cangwei)) {
//            etKucun.setText("");
//            ToastUtils.show("请等待上一个条码才可以进行下一条码扫描");
//            return true;
//        }
        String search = v.getText().toString();
        etKucun.setText("");
        if (TextUtils.isEmpty(search)) {
            ToastUtils.show("无效二维码");
            return true;
        }
        //产品二维码
        if (search.indexOf(Constant.KuFangkey) == 0) {
//            cangwei = search;
//            tvCwtm.setText(cangwei);
//            tvCpCount.setText("");
            if (!TextUtils.isEmpty(search)) {
                doSearchServer(search);
            }
        }else  if (search.indexOf(Constant.PaiGongDanKey) == 0) {
            if (!TextUtils.isEmpty(search)) {
                doSearchServer(search);
            }
        }else  if (search.indexOf(Constant.DAKuFangkey) == 0) {
            if (!TextUtils.isEmpty(search)) {
                doSearchServer(search);
            }
        }
        else {
            ToastUtils.show("无效二维码");
            return true;
        }

        return false;
    }

    //查询这边不过滤C S
    private void doSearchServer(final String mcangwei) {
        Api.searhchanpin(getActivity(), mcangwei, new NetUtils.NetCallBack<ServiceResult>() {
            @Override
            public void success(ServiceResult rspData) {
                    if(rspData.getRecode()==1){
                        SearchBeenInfo searchBeenInfo= (SearchBeenInfo) rspData;
                        List<RukuBeen> rukuBeenList=searchBeenInfo.getInfo();
                        homeAdapter.setNewData(rukuBeenList);
                        tvCount.setText(homeAdapter.getData().size()+"");
                    }
                    else{
                        homeAdapter.setNewData(null);
                        ToastUtils.show("没有查找到数据");
                    }
            }

            @Override
            public void failed(String msg) {
                ToastUtils.show(msg);
            }

            @Override
            public void onUsedCache(String msg) {

            }
        },SearchBeenInfo.class);
    }


    private void initData() {

        etKucun.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if (actionId == 0 || actionId == 6) {
                    if (event == null) {
                        if (actionOk(v)) return true;
                    } else {
                        switch (event.getAction()) {
                            case KeyEvent.ACTION_UP:
                                // Log.i("churu","event ACTION_UP"+",actionId="+actionId);
                                if (actionOk(v)) return true;
                        }
                    }
                }
                return true;

            }
        });

    }

    private void initAdapter() {
        rvList.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvList.addItemDecoration(new SpaceItemDecoration(getActivity()).setSpace(0.5f).setSpaceColor(getResources().getColor(R.color.item_press2)));
        homeAdapter = new SearchAdapter();
        homeAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_name:
                        break;
                }
            }
        });
        homeAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, final int position) {

                return false;
            }
        });
        homeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, final int position) {

            }
        });
        homeAdapter.setEmptyView(R.layout.loading_view, (ViewGroup) rvList.getParent());
        rvList.setAdapter(homeAdapter);

        homeAdapter.setNewData(null);
    }

    private void initView() {

        etKucun.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                etKucun.setInputType(InputType.TYPE_CLASS_TEXT);
                return false;
            }
        });
        etKucun.setInputType(InputType.TYPE_NULL);
    }

}
